<?php

namespace app\admin\model;
use think\Model;


/**
 * This is the model class for table "hxdc_xcx_agent".
 *
 * @property int $id
 * @property int $member_id 用户id
 * @property int $kf_id 推荐顾问id
 * @property int $agent_type 经纪人类型、3自由、4自渠、2业主、1员工
 * @property string $nickname 昵称
 * @property string $mobile 手机号
 * @property string $head_portrait 头像
 * @property string $company 公司
 * @property string $department 部门
 * @property string $address 地址
 * @property string $card_type 证件类型
 * @property string $card_number 证件号码
 * @property string $card_positive 证件正面图
 * @property string $card_obverse 证件反面图
 * @property string $bank_realname 持卡人姓名
 * @property string $bank_type 银行卡类型
 * @property string $bank_number 银行卡号
 * @property string $bank_bankname 开户银行
 * @property string $bank_address 开户地
 * @property string $bank_sonname 支行名称
 * @property string $bank_img 银行卡照片
 * @property string $signature 手写签名图
 * @property int $created_at 注册时间
 * @property int $updated_at 最后一次修改时间
 * @property int $status 	状态[-1:删除;0:禁用;1启用]
 * @property int $merchant_id 商户id
 */
class XcxAgent extends Model
{
    public static function paramsNew() :array {
        return array(
            'member_id' => '',
            'kf_id' => '',
            'agent_type' => '',
            'nickname' => '',
            'mobile' => '',
            'head_portrait' => '',
            'company' => '',
            'department' => '',
            'address' => '',
            'card_type' => '',
            'card_number' => '',
            'card_positive' => '',
            'card_obverse' => '',
            'bank_realname' => '',
            'bank_type' => '',
            'bank_number' => '',
            'bank_bankname' => '',
            'bank_address' => '',
            'bank_sonname' => '',
            'bank_img' => '',
            'signature' => '',
            'merchant_id' => 0,
            'status' => 1,
            'created_at' => time(),
            'updated_at' => time(),
            'agentid' => '',
        );
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '用户 ID',
            'kf_id' => '推荐顾问 ID',
            'agent_type' => '经纪人类型',
            'nickname' => '经纪人姓名',
            'mobile' => '手机号',
            'head_portrait' => '头像',
            'company' => '公司',
            'department' => '部门',
            'address' => '地址',
            'card_type' => '证件类型',
            'card_number' => '证件号码',
            'card_positive' => '证件正面照',
            'card_obverse' => '证件反面照',
            'bank_realname' => '持卡人姓名',
            'bank_type' => '银行卡类型',
            'bank_number' => '银行卡号码',
            'bank_bankname' => '开户银行',
            'bank_address' => '开户地址',
            'bank_sonname' => '支行名称',
            'bank_img' => '银行卡正面图',
            'signature' => '签名图',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'status' => '状态',
            'merchant_id' => '商户 ID',
        ];
    }
    /**
     * 关联会员
     */
    public function getMember()
    {
        return $this->hasOne(Member::class, ['id' => 'member_id']);
    }

    /**
     * 推荐顾问
     */
    public function getKf()
    {
        return $this->hasOne(XcxKf::class, ['id' => 'kf_id']);
    }

    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
